package com.onec.service.customer.service.online;


import com.onec.service.api.dto.request.gateway.BizContent;
import com.onec.service.api.dto.response.customer.MerchantResp;
import com.onec.service.api.utils.ValidatorUtils;
import com.onec.service.api.utils.group.MerchantOpenGroup;
import com.onec.service.customer.entity.agent.AgentInfo;
import com.onec.service.customer.entity.merchant.MerchantBusiness;
import com.onec.service.customer.entity.merchant.MerchantInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 下游商户注册逻辑：
 * 缺陷：：：
 *
 * @author ONEC
 */
@Service("merchant_business_open_service")
public class MerchantBusinessOpenService extends BaseOnlineDefaultService {

    private static final Logger logger = LoggerFactory.getLogger(MerchantBusinessOpenService.class);

    @Override
    public MerchantResp execute(BizContent bizContent) {
        // 简单参数验证：
        ValidatorUtils.validateEntity(bizContent, MerchantOpenGroup.class);
        // 判断商户在机构下的注册上限：
        AgentInfo agentInfo = agentInfoService.getAvailableAgent(bizContent.getAgentNo());

        this.checkAgentNo(bizContent.getAgentNo(), agentInfo.getAgentCode());

        this.validateProductFee(bizContent, agentInfo.getId());
        //
        MerchantInfo merchantInfo = merchantInfoService.getAvailableMerchant(bizContent.getMerchantNo());

        MerchantBusiness merchantBusiness = merchantBusinessService.createMerchantBusiness(bizContent, merchantInfo);
        // 将数据提交到需要报备的上游： 异步提交：
//        List<ChannelInfo> channelInfos = channelInfoService.getAvailableChannelInfoProduct(merchantInfo.getProduct());
//        if (channelInfos.size() > 0) {
//            for (ChannelInfo channelInfo : channelInfos) {
//                String channelName = this.getChannelMerchant(channelInfo.getCode());
//                MerchantInfoParam merchantInfoParam = new MerchantInfoParam(bizContent, merchantInfo, channelInfo);
//                ResponseMerchant responseMerchant = channelMerchant.get(channelName).register(merchantInfoParam);
//            }
//        }
        return this.respOpenProduct(merchantBusiness, bizContent);
    }
}
